<?php
namespace app\admin\controller;

use app\common\controller\Admin;
use think\facade\View;

class Config extends Admin
{
    protected $modelClass = '\app\common\model\Config';
    
    protected function setLang()
    {
        $this->searchArr = [
            'k' => lang('k'),
            'v' => lang('v'),
            'infos' => lang('infos'),
            'type' => lang('type'),
            'texttype' => lang('texttype'),
        ];
    }
    
    public function index()
    {
        $this->_indexSearch();
        
        $where = [];
        $getSearch = $this->_getSearch();
        if (!empty($getSearch)) $where[] = $getSearch;
        
        if (input('get._sort')){
            $order = explode(',', input('get._sort'));
            $order = $order[0].' '.$order[1];
        }else{
            $order = 'type asc,status desc,sorts asc,id asc';
        }
        $dataList = $this->cModel->where($where)->order($order)->paginate('', false, page_param());
        
        View::assign('dataList', $dataList);
        return View::fetch();
    }
    
    public function save()
    {
        if (request()->isPost()){
            $data = input('post.', '', 'htmlspecialchars');
            $type = $data['type'];  //取出类型
            unset($data['type']);
            if(!empty($type)){
                if(is_array($data) && !empty($data)){
                    foreach ($data as $k=>$val) {
                        if (is_array($val)){
                            $val = implode(',', del_arr_empty($val));
                        }
                        $where = ['type' => $type, 'k' => $k];
                        $this->cModel->where($where)->save(['v' => $val]);
                    }
                    $this->dbconfig();
                    return ajax_return(lang('action_success'), url('Config/'.$type));
                }else{
                    return ajax_return(lang('action_fail'));
                }
            }else{
                return ajax_return(lang('action_fail'));
            }
        }
    }
    
    private function dbconfig()
    {
        $dbconfig_path = app()->getConfigPath().'dbconfig.php';
        $data = $this->cModel->field('k, v, type')->where([['status', '=', 1]])->order('type asc,sorts asc')->select();
        $type = "";
        $close = false;
        $str = "<?php\r\nreturn [";
        if (!empty($data)){
            foreach ($data as $k => $v){
                if (isset($v['v'][0]) && $v['v'][0] == '['){
                    $v['v'] = htmlspecialchars_decode($v['v']);
                }else{
                    $v['v'] = "'".$v['v']."'";
                }
                if ($type == $v['type']){
                    $str .= "\r\n        '".$v['k']."' => ".$v['v'].",";
                }else{
                    $type = $v['type'];
                    $str .= $close ? "\r\n    ]," : "";
                    $close = true;
                    $str .= "\r\n    '".$v['type']."' => [";
                    $str .= "\r\n        '".$v['k']."' => ".$v['v'].",";
                }
            }
        }
        $str .= "\r\n    ],\r\n];\r\n";
        if (!file_put_contents($dbconfig_path, $str)) {
            ajax_return(lang('dbconfig_error'));
        }
    }
    
    public function web()
    {
        $type = ACTION_NAME;
        $where = [['type', '=', $type], ['status', '=', 1]];
        $data = $this->cModel->where($where)->order('sorts ASC,id ASC')->select();
        foreach ($data as $key => $val){
            $val[$val->k] = $val->v;
        }
        View::assign('data', $data);
        View::assign('type', ['type' => $type]);
        return View::fetch('save');
    }
    
    public function up()
    {
        $type = ACTION_NAME;
        $where = [['type', '=', $type], ['status', '=', 1]];
        $data = $this->cModel->where($where)->order('sorts ASC,id ASC')->select();
        foreach ($data as $key => $val){
            $val[$val->k] = $val->v;
        }
        View::assign('data', $data);
        View::assign('type', ['type' => $type]);
        return View::fetch('save');
    }
    
    public function sys()
    {
        $type = ACTION_NAME;
        $where = [['type', '=', $type], ['status', '=', 1]];
        $data = $this->cModel->where($where)->order('sorts ASC,id ASC')->select();
        foreach ($data as $key => $val){
            $val[$val->k] = $val->v;
        }
        View::assign('data', $data);
        View::assign('type', ['type' => $type]);
        return View::fetch('save');
    }
}